/*	
 *  Copyright (c) 2009-@year@. The GUITAR group at the University of Maryland. Names of owners of this group may
 *  be obtained by sending an e-mail to atif@cs.umd.edu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 *  documentation files (the "Software"), to deal in the Software without restriction, including without 
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 *	the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
 *	conditions:
 * 
 *	The above copyright notice and this permission notice shall be included in all copies or substantial 
 *	portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 *	LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
 *	EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 *	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 *	THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */
package edu.umd.cs.imut.executor;

import java.util.List;

import edu.umd.cs.imut.exception.IException;
import edu.umd.cs.imut.executor.criteria.IIdentificationCriteria;
import edu.umd.cs.imut.executor.matching.IDataMatchingCriteria;
import edu.umd.cs.imut.executor.matching.IMatchingCriteria;
import edu.umd.cs.imut.model.IApplication;
import edu.umd.cs.imut.model.IComponent;
import edu.umd.cs.imut.model.data.ComponentType;

/**
 * Execute the application using a certain algorithm, controlled by the
 * <code>performNextEvent</code> method
 * 
 * <p>
 * 
 * @author <a href="mailto:baonn@cs.umd.edu"> Bao N. Nguyen </a>
 * 
 */
abstract public class IExecutor {
	/**
	 * @return
	 * @see edu.umd.cs.imut.model.IApplication#getAllComponents()
	 */
	public List<IComponent> getAllComponents() {
		return application.getAllComponents();
	}

	/**
	 * @return
	 * @see edu.umd.cs.imut.model.IApplication#getAllEventComponents()
	 */
	public List<IComponent> getAllEventComponents() {
		return application.getAllEventComponents();
	}

	protected IApplication application;

	/**
	 * @param application
	 */
	public IExecutor(IApplication application) {
		super();
		this.application = application;
	}

	/**
	 * perform next event selected by the executor algorithm
	 * 
	 * @throws IException
	 */
	abstract public void performNextEvent() throws IException;

	/**
	 * true if there is an event to perform
	 * 
	 * @return
	 */
	abstract public boolean hasNextEvent();

	/**
	 * starts the executor
	 */
	public void starts() {
		this.application.connect();
	}

	/**
	 * terminates the executor
	 */
	public void terminates() {
		this.application.disconnect();
	}

	/**
	 * search for a component on the current GUI with the provided searching
	 * criteria
	 * 
	 * <p>
	 * 
	 * @param criteria
	 *            searching criteria
	 * @return component to search if there is and null if there is not
	 * @see IIdentificationCriteria
	 */
	public IComponent searchComponent(IIdentificationCriteria criteria) {
		List<IComponent> allComponents = getAllComponents();
		for (IComponent component : allComponents) {
			if (criteria.isMatched(component))
				return component;
		}
		return null;
	}

	/**
	 * search for a component on the current GUI with the provided searching
	 * criteria and the off-line component data
	 * 
	 * @param dataComponent
	 *            the the off-line component data
	 * @param criteria
	 *            searching criteria
	 * @return
	 */
	public IComponent searchComponent(ComponentType dataComponent,
			IDataMatchingCriteria criteria) {
		List<IComponent> allComponents = getAllComponents();
		for (IComponent component : allComponents) {
			if (criteria.isMatched(component))
				return component;
		}
		return null;
	}

}
